###################################
#Figure 6a
###################################


library(foreign)
library(lattice)
library(Hmisc)
library(readstata13)
library(latticeExtra)

data<-read.dta13("Figure6a1_Preds_reshaped.dta")
data2<-read.dta13("Figure6a2_Preds_reshaped.dta")
days<-data[,1]
CycleLowBound1<-data[,2]
CycleUpBound1<-data[,3]
CycleLowBound32<-data[,4]
CycleUpBound32<-data[,5]
PostLowBound1<-data2[,2]
PostUpBound1<-data2[,3]
PostLowBound32<-data2[,4]
PostUpBound32<-data2[,5]

group<-c("Linear Cycle Trend", "Post-1994 Election Indicator")

x<-1:735

pdf(file="Figure6a.pdf", family="Bookman")
setTrellis()
xyplot(CycleLowBound1 ~ days | group, type="l", xlab='Days to Next Election', ylab='Probability of Party Unity Vote', xlim=rev(range(x)), ylim=c(.1, .8), col=c("white"), key=list(text=list(c("Early in Time Series", "Late In Time Series")), points=list(pch=c(15, 15), col=c("gray35", "gray70")), columns=1))

a<-c(days, rev(days))
b<-c(CycleLowBound1, rev(CycleUpBound1))
c<-c(CycleLowBound32, rev(CycleUpBound32))
d<-c(PostLowBound1, rev(PostUpBound1))
e<-c(PostLowBound32, rev(PostUpBound32))

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=b, col="gray35", border=NA))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=c, col="gray70", border=NA))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=d, col="gray35", border=NA))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=e, col="gray70", border=NA))
trellis.unfocus( )

dev.off()


##########################################
#Figure 6b
##########################################

#See VoteTimingDisaggCycle_Preds835_CycleSpecMeans_Session and VoteTimingDisaggPost_Preds835_PostSpecMeans_Session for values below

library(foreign)
library(lattice)
library(Hmisc)
library(readstata13)
library(latticeExtra)

data<-read.dta13("Figure6b_Preds_reshaped.dta")
days<-data[,1]
Means<-data[,2]

group<-c("Linear Cycle Trend", "Post-1994 Election Indicator")

pdf(file="Figure6b.pdf", family="Bookman")
setTrellis()
xyplot(Means ~ days | group, xlab='Session', ylab='Probability of Party Unity Vote', xlim=c(-.5, 1.5), scales=list(x=list(at=c(0,1), labels=c("First", "Second"))), ylim=c(.1, .8), col=c("white"), key=list(text=list(c("Early in Time Series", "Late In Time Series")), points=list(pch=c(15, 15), col=c("gray35", "gray70")), columns=1))

panel.axis(side="bottom", labels=FALSE)

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, .2806137, 0, .3110787, col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(1, .1994956, 1, .2250505, col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=.2957543, col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=.2120065, col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, .2957543, 1, .2120065, col="gray35", lty=2))
trellis.unfocus( )

#

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, .6558279, 0, .6823965, col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(1, .6826357, 1, .7119207, col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=.6693468, col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=.6970839, col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, .6693468, 1, .6970839, col="gray70", lty=2))
trellis.unfocus( )

#

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, .4653562, 0, .4846187, col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(1, .4043796, 1, .4235292, col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=.4749912, col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=.4138451, col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, .4749912, 1, .4138451, col="gray35", lty=2))
trellis.unfocus( )

#

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, .5805957, 0, .5960964, col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(1, .592508, 1, .6123582, col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=.5883651, col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=.6023708, col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, .5883651, 1, .6023708, col="gray70", lty=1))
trellis.unfocus( )

#

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.4, .77, -.3, .77, col="black", lty=2))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.text", list(x=.5, y=.77, labels="Statistically significant difference", cex=.8))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.45, .79, 1.45, .79, col="black", lty=1))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.45, .75, 1.45, .75, col="black", lty=1))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.45, .79, -.45, .75, col="black", lty=1))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(1.45, .79, 1.45, .75, col="black", lty=1))
trellis.unfocus( )

dev.off()
